package pl.uz.zgora.plan;

import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class StartPageActivity extends Activity {
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start_page);

		final PrefrencesManager pm = PrefrencesManager.getInstance(this);
		final Boolean hasData = pm.getBooleanFromPreferences("hasData");
		
		try {
			if (hasData) {
				final Intent intent = new Intent(this, MainMenuActivity.class);
				final Bundle intentParams = getIntent().getExtras();
				
				try {
					if (intentParams != null) {
						boolean deleteDB = intentParams
								.getBoolean("forceDBDelete");
						if (deleteDB) {
							new PlanUzDataSource(this, true);
						} else {
							startActivity(intent);
							this.finish();
						}
					} else {
						startActivity(intent);
						this.finish();
					}
				} catch (android.content.ActivityNotFoundException e) {
					HelperMethodsManager.manageException(
							"StartPageActivity::onCreate()",
							e.getLocalizedMessage(), StartPageActivity.this);
				}
			} else {
				new PlanUzDataSource(this, true);
			}
		} catch (SQLException e) {
			HelperMethodsManager.manageException("StartPageActivity::onCreate()", e.getLocalizedMessage(), this);
		} catch (Exception e) {
			HelperMethodsManager.manageException("StartPageActivity::onCreate()", e.getLocalizedMessage(), this);
		}
        
        setupUI();
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == 54321) {
			if(resultCode == 0){
				//finish();
			}
		}
	}
	
	private void setupUI() {
		final PrefrencesManager prefsManager = PrefrencesManager.getInstance(this);
		// Przycisk "Jestem studentem"
		final Button studentButton = (Button)findViewById(R.id.student_mode_btn);
		studentButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				prefsManager.addStringToPreferences("appmode", "student");
				final Intent intent = new Intent(v.getContext(), SelectStudentDataActivity.class);
				try {
					startActivityForResult(intent, 54321);
					finish();
				} catch (android.content.ActivityNotFoundException e) {
					HelperMethodsManager.manageException("StartPageActivity::setupUI()",
							e.getLocalizedMessage(), StartPageActivity.this);
				}
			}
		});
		// Przycisk "Jestem wykładowcą"
		final Button teacherButton = (Button)findViewById(R.id.teacher_mode_btn);
		teacherButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				prefsManager.addStringToPreferences("appmode", "teacher");
				final Intent intent = new Intent(v.getContext(), SelectTeacherDataActivity.class);
				try {
					startActivityForResult(intent, 54321);
					finish();
				} catch (android.content.ActivityNotFoundException e) {
					HelperMethodsManager.manageException("StartPageActivity::setupUI()",
							e.getLocalizedMessage(), StartPageActivity.this);
				}
			}
		});
	}
}
